import apis from '../../http/apis/apis';
export default {
    namespaced: true,
    state: {
        parkingsData: {
            data: [],
            currentPage: 1,
            totalPage: 1,
            totalNum: 0
        },
        pageSize: 3,
        searchData: {
            options: 'address',
            search_data: '',
        },
        loading: false,
    },
    mutations: {
        //分页，上下页
        changCurrentPage(state, val) {
            state.parkingsData.currentPage = val - 0;
        },
        //改变每页显示多少条数据
        changePageSize(state, val) {
            state.parkingsData.currentPage = 1;
            state.pageSize = val;
        },
        //根据获取的数据更改state的parkingsData
        changeParkingsData(state, val) {
            state.parkingsData = { ...val };
        },

        changeSearchData(state, val) {
            state.searchData = { ...val }
        },

        //改变加载动画
        changeLoading(state, val) {
            state.loading = !state.loading
        },

    },
    actions: {
        //获取停车位
        async getParkings(context) {
            //发送请求时就加载动画
            context.commit('changeLoading')
            const { data } = await apis.parkings.getParkings({
                ...context.state.searchData,
                currentPage: context.state.parkingsData.currentPage,
                pageSize: context.state.pageSize,
            });
            if (data.status) {
                context.commit('changeParkingsData', data.data)
                //请求成功加载动画结束
                context.commit('changeLoading')

            }
        },

        //添加停车位 冰
        async addParking(context, parkings) {
            const { data } = await apis.parkings.addParking({
                parkings
            });
            return data;
        },

        //车位上下架 冰
        async amendState(context, val) {
            const { data } = await apis.parkings.amendState({
                val
            });
        },

        //分页，上下页

        //删除停车位
        async deleteParkings(context, val) {
            const { data } = await apis.parkings.deleteParkings({
                _id: val
            })
            // console.log(data);
            if (data.status) {
                context.dispatch('getParkings')
            }

            return data
        },

        //查询是否可修改，判断用户
        async checkCanChangeModify(context, val) {
            const { data } = await apis.parkings.checkCanChangeModify({
                _id: val
            })
            return data
        },

        //确认修改停车位
        async modifyParkings(context, val) {

            const { data } = await apis.parkings.modifyParkings({
                ...val
            })
            if (data.status) {
                context.dispatch('getParkings')
            }
            return data;
        },
    }
}